package com.n4j.syncit.service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.gdata.client.blogger.BloggerService;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Feed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import com.n4j.syncit.entity.Blog;

public class BlogService {
	private static final String METAFEED_URL = 
	      "http://www.blogger.com/feeds/default/blogs";

	private static final String FEED_URI_BASE = "http://www.blogger.com/feeds";
	private static final String POSTS_FEED_URI_SUFFIX = "/posts/default";
	private static final String COMMENTS_FEED_URI_SUFFIX = "/comments/default";
	
	public void sync(Blog blog){
		saveBlog(blog);
		sync2Blogger(blog);
		sync2Wp(blog);
	}
	
	private void saveBlog(Blog blog){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Entity blogEntity = new Entity("Blog");
		blogEntity.setProperty("title", blog.getTitle());
		blogEntity.setProperty("content", blog.getContent());
		blogEntity.setProperty("tag", blog.getTag());
		Date current = new Date();
		blogEntity.setProperty("updateDate", current);
		blogEntity.setProperty("sync2wp", false);
		blogEntity.setProperty("sync2blogger", false);
		datastore.put(blogEntity);
	}
	
	public void sync2Blogger(Blog blog){
		
		
	}
	
	public void sync2Wp(Blog blog){
		
	}
	
	private void setup(){
		String userName="testapp001@gmail.com";
		String pwd="*Pass1234";
		BloggerService myService=new BloggerService("wenwei-syncit-1");
		
		try {
			myService.setUserCredentials(userName, pwd);
			final URL metaUrl = new URL(METAFEED_URL);
		    Feed metaFeed = myService.getFeed(metaUrl, Feed.class);

		    // If the user has a blog then return the id (which comes after 'blog-')
		    String blogId="";
		    if (metaFeed.getEntries().size() > 0) {
		      Entry entry = metaFeed.getEntries().get(0);
		      blogId= entry.getId().split("blog-")[1];
		    }
			String feedUri = FEED_URI_BASE + "/" + blogId;
			URL feedUrl = new URL(feedUri + POSTS_FEED_URI_SUFFIX);
		    Feed resultFeed = myService.getFeed(feedUrl, Feed.class);
		    // If the user has a blog then return the id (which comes after 'blog-')
		    
		    // Print the results
		    System.out.println(resultFeed.getTitle().getPlainText());
		    for (int i = 0; i < resultFeed.getEntries().size(); i++) {
		      Entry entry = resultFeed.getEntries().get(i);
		      System.out.println("title:\t" + entry.getTitle().getPlainText());
		      System.out.println("content:\t" + entry.getXmlBlob().getFullText());
		    }
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args){
		System.setProperty("http.proxyHost", "192.168.152.115");
		System.setProperty("http.proxyPort", "6080");
		System.setProperty("https.proxyHost", "192.168.152.115");
		System.setProperty("https.proxyPort", "6080");
		BlogService bs=new BlogService();
		bs.setup();
	}

}
